# Set MySQL Password Script
Write-Host "Setting MySQL root password..." -ForegroundColor Green

# Find MySQL client
$possiblePaths = @(
  "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe",
  "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe",
  "C:\ProgramData\chocolatey\lib\mysql\tools\mysql.exe"
)

$mysqlPath = $null
foreach ($path in $possiblePaths) {
  if (Test-Path $path) {
    $mysqlPath = $path
    Write-Host "Found MySQL client at: $mysqlPath" -ForegroundColor Green
    break
  }
}

if ($mysqlPath) {
  # Set password to root123
  Write-Host "Setting password to 'root123'..." -ForegroundColor Yellow
    
  try {
    # Connect without password and set new password
    & $mysqlPath -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';"
    & $mysqlPath -u root -e "FLUSH PRIVILEGES;"
        
    Write-Host "Password set successfully!" -ForegroundColor Green
        
    # Test the new password
    Write-Host "Testing new password..." -ForegroundColor Yellow
    & $mysqlPath -u root -proot123 -e "SELECT 'Password test successful!' as result;"
        
    if ($LASTEXITCODE -eq 0) {
      Write-Host "Password test passed!" -ForegroundColor Green
      Write-Host "New connection details:" -ForegroundColor Cyan
      Write-Host "Username: root" -ForegroundColor White
      Write-Host "Password: root123" -ForegroundColor White
    }
    else {
      Write-Host "Password test failed!" -ForegroundColor Red
    }
        
  }
  catch {
    Write-Host "Error setting password: $($_.Exception.Message)" -ForegroundColor Red
  }
}
else {
  Write-Host "MySQL client not found!" -ForegroundColor Red
  Write-Host "Trying alternative method..." -ForegroundColor Yellow
    
  # Alternative: Try to find mysql in PATH
  $mysqlCmd = Get-Command mysql -ErrorAction SilentlyContinue
  if ($mysqlCmd) {
    Write-Host "Found MySQL in PATH, setting password..." -ForegroundColor Green
        
    try {
      mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';"
      mysql -u root -e "FLUSH PRIVILEGES;"
            
      Write-Host "Password set successfully!" -ForegroundColor Green
            
      # Test the new password
      mysql -u root -proot123 -e "SELECT 'Password test successful!' as result;"
            
      if ($LASTEXITCODE -eq 0) {
        Write-Host "Password test passed!" -ForegroundColor Green
        Write-Host "New connection details:" -ForegroundColor Cyan
        Write-Host "Username: root" -ForegroundColor White
        Write-Host "Password: root123" -ForegroundColor White
      }
      else {
        Write-Host "Password test failed!" -ForegroundColor Red
      }
            
    }
    catch {
      Write-Host "Error setting password: $($_.Exception.Message)" -ForegroundColor Red
    }
  }
  else {
    Write-Host "MySQL client not found in PATH either!" -ForegroundColor Red
    Write-Host "Please set the password manually in Beekeeper Studio:" -ForegroundColor Yellow
    Write-Host "1. Connect with empty password" -ForegroundColor White
    Write-Host "2. Run: ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';" -ForegroundColor White
    Write-Host "3. Run: FLUSH PRIVILEGES;" -ForegroundColor White
  }
}

Write-Host "Password setting process completed!" -ForegroundColor Green
